/**
 * Created by wxb on 2016/10/23.
 */
'use strict';

angular.module('BlurAdmin')
  .directive('escKey', function () {
    return function (scope, element, attrs) {
      element.bind('keydown keypress', function (event) {
        if(event.which === 27) { // 27 = esc key
          scope.$apply(function (){
            scope.$eval(attrs.escKey);
          });

          event.preventDefault();
        }
      });
    };
  })
  .directive('convertToNumber', function() {
    return {
      require: 'ngModel',
      link: function(scope, element, attrs, ngModel) {
        ngModel.$parsers.push(function(val) {
          if (val && val !== '') {
            return parseInt(val, 10);
          }
          else {
            return val
          }
        });
        ngModel.$formatters.push(function(val) {
          if (val || val == 0) {
            return '' + val;
          }
          else {
            return ''
          }
        });
      }
    };
  })
  .directive('fileChange', function() {
    return {
      restrict: 'A',
      scope: {
        handler: '&'
      },
      link: function (scope, element) {
        element.on('change', function (event) {
          scope.$apply(function(){
            scope.handler({files: event.target.files});
          });
        });
      }
    };
  })
  .directive('csSelect', function () {
    return {
      require: '^stTable',
      template: '<input type="checkbox"/>',
      scope: {
        row: '=csSelect'
      },
      link: function (scope, element, attr, ctrl) {
        element.bind('change', function (evt) {
          scope.$apply(function () {
            ctrl.select(scope.row, 'multiple');
          });
        });

        scope.$watch('row.isSelected', function (newValue, oldValue) {
          if (newValue === true) {
            element.parent().addClass('st-selected');
          } else {
            element.parent().removeClass('st-selected');
          }
        });
      }
    };
  })
  .directive('attrClick', function(){
    return {
      restrict: 'A',
      scope: {click: '&', arg: '='},
      link: function(scope, element, attrs) {
        element.bind('click',
          function () {
            scope.click()(scope.arg)
          }
        );
      }
    }
  })
  .directive('countdown', [
    'Util',
    '$interval',
    function (Util, $interval) {
      return {
        restrict: 'A',
        scope: { date: '@' },
        link: function (scope, element) {
          var future;
          future = moment(scope.date).valueOf();
          $interval(function () {
            var diff;
            diff = Math.floor((future - Date.now()) / 1000);
            return element.text(Util.dhms(diff));
          }, 1000);
        }
      };
    }
  ])
  .directive('countup', [
    'Util',
    '$interval',
    function (Util, $interval) {
      return {
        restrict: 'A',
        scope: { date: '@' },
        link: function (scope, element) {
          var future;
          future = moment(scope.date).valueOf();
          $interval(function () {
            var diff;
            diff = Math.floor((Date.now() - future) / 1000);
            return element.text(Util.dhms(diff));
          }, 1000);
        }
      };
    }
  ])
  .factory('Util', [function () {
  return {
    dhms: function (t) {
      var days, hours, minutes, seconds;
      days = Math.floor(t / 86400);
      t -= days * 86400;
      hours = Math.floor(t / 3600) % 24;
      t -= hours * 3600;
      minutes = Math.floor(t / 60) % 60;
      t -= minutes * 60;
      seconds = t % 60;
      return [
        days > 0 ? (days + '天') : '',
        hours > 0 ? (hours + '小时') : '',
        minutes > 0 ? (minutes + '分') : '' ,
        seconds + '秒'
      ].join(' ');
    }
  };
}])